package mapreduce.wordcount;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * mapreduce入门案例：统计wordcount.txt文件中单词出现次数
 */
public class JobMain extends Configured implements Tool {
    public int run(String[] strings) throws Exception {
        Job job = Job.getInstance(super.getConf(), JobMain.class.getSimpleName());
//打包到集群上面运行时候，必须要添加以下配置，指定程序的main函数
        job.setJarByClass(JobMain.class);
//第一步：读取输入文件解析成key，value对
        job.setInputFormatClass(TextInputFormat.class);
        //TextInputFormat.addInputPath(job,new Path("hdfs://192.168.100.100:8020/wordcount"));
        TextInputFormat.addInputPath(job,new Path("file:///C:\\Users\\xurui\\Desktop\\wordcount.txt"));    //可以只指定到文件夹。只指定目录的话该目录下所有文件都会读取。
//第二步：设置我们的mapper类
        job.setMapperClass(WordCountMapper.class);
//设置我们map阶段完成之后的输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
//第三步，第四步，第五步，第六步，省略
//第七步：设置我们的reduce类
        job.setReducerClass(WordCountReducer.class);
//设置我们reduce阶段完成之后的输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
//第八步：设置输出类以及输出路径
        job.setOutputFormatClass(TextOutputFormat.class);
        //TextOutputFormat.setOutputPath(job,new Path("hdfs://192.168.100.100:8020/wordcount_out"));
        TextOutputFormat.setOutputPath(job,new Path("file:///C:\\Users\\xurui\\Desktop\\output"));    //指定的目录不能存在
        boolean b = job.waitForCompletion(true);
        return b?0:1;
    }

    /**
     * 程序main函数的入口类
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        Configuration configuration = new Configuration();
        configuration.set("mapreduce.framework.name","local");    //本地模式
        configuration.set(" yarn.resourcemanager.hostname","local");    //本地模式
        Tool tool = new JobMain();
        int run = ToolRunner.run(configuration, tool, args);
        System.exit(run);
    }
}
